<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- qml-extending.qdoc -->
  <title>Extending QML - Default Property Example | Qt QML 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtqml-index.html">Qt QML</a></td><td ><a href="qmlextendingexamples.html">Qt QML Examples</a></td><td >
			扩展QML -默认属性示例</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtqml-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><span lang="zh-cn">目录</span></h3>
<ul>
<li class="level1"><a href="#declaring-the-birthdayparty-class">声明BirthdayParty 
类</a></li>
<li class="level1"><a href="#running-the-example">运行该示例</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">扩展QML -默认属性示例</h1>
<span class="subtitle"></span>
<!-- $$$referenceexamples/default-brief -->
<p>默认属性。</p>
<!-- @@@referenceexamples/default -->
<!-- $$$referenceexamples/default-description -->
<div class="descr"> <a name="details"></a>
<p>这个例子建立在:</p>
<ul>
<li><a href="qtqml-referenceexamples-coercion-example.html">扩展QML -继承和强制示例</a></li>
<li><a href="qtqml-referenceexamples-properties-example.html">扩展QML -对象和列表属性类型示例</a></li>
<li><a href="qtqml-referenceexamples-adding-example.html">扩展QML -添加类型示例</a></li>
</ul>
<p>默认属性示例是对<a href="qtqml-referenceexamples-coercion-example.html">扩展QML 
-继承和强制示例</a>的一个小修改，该示例通过使用默认属性简化了BirthdayParty的规范。</p>
<pre class="qml">

  <span class="type">BirthdayParty</span> {
      <span class="name">host</span>: <span class="name">Boy</span> {
          <span class="name">name</span>: <span class="string">&quot;Bob Jones&quot;</span>
          <span class="name">shoeSize</span>: <span class="number">12</span>
      }

      <span class="type">Boy</span> { <span class="name">name</span>: <span class="string">&quot;Leo Hodges&quot;</span> }
      <span class="type">Boy</span> { <span class="name">name</span>: <span class="string">&quot;Jack Smith&quot;</span> }
      <span class="type">Girl</span> { <span class="name">name</span>: <span class="string">&quot;Anne Brown&quot;</span> }
  }

</pre>
<a name="declaring-the-birthdayparty-class"></a>
<h2 id="declaring-the-birthdayparty-class"><span lang="zh-cn">声明</span>BirthdayParty
<span lang="zh-cn">类</span></h2>
<p>这个示例与上一个示例的唯一区别是添加了<code>DefaultProperty</code> 类信息注释。</p>
<pre class="cpp">

  <span class="keyword">class</span> BirthdayParty : <span class="keyword">public</span> <span class="type"><a href="../qtcore/qobject.html">QObject</a></span>
  {
      Q_OBJECT
      Q_PROPERTY(Person <span class="operator">*</span>host READ host WRITE setHost)
      Q_PROPERTY(<span class="type"><a href="qqmllistproperty.html">QQmlListProperty</a></span><span class="operator">&lt;</span>Person<span class="operator">&gt;</span> guests READ guests)
      Q_CLASSINFO(<span class="string">&quot;DefaultProperty&quot;</span><span class="operator">,</span> <span class="string">&quot;guests&quot;</span>)
  <span class="keyword">public</span>:
      BirthdayParty(<span class="type"><a href="../qtcore/qobject.html">QObject</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);

      Person <span class="operator">*</span>host() <span class="keyword">const</span>;
      <span class="type">void</span> setHost(Person <span class="operator">*</span>);

      <span class="type"><a href="qqmllistproperty.html">QQmlListProperty</a></span><span class="operator">&lt;</span>Person<span class="operator">&gt;</span> guests();
      <span class="type">int</span> guestCount() <span class="keyword">const</span>;
      Person <span class="operator">*</span>guest(<span class="type">int</span>) <span class="keyword">const</span>;

  <span class="keyword">private</span>:
      Person <span class="operator">*</span>m_host;
      <span class="type"><a href="../qtcore/qlist.html">QList</a></span><span class="operator">&lt;</span>Person <span class="operator">*</span><span class="operator">&gt;</span> m_guests;
  };

</pre>
<p>
默认属性指定了当未指定显式属性时要分配的属性，对于BirthdayParty，类型为guest属性。这纯粹是一种语法简化，其行为与按名称指定属性相同，但在许多情况下可以增加一种更自然的感觉。默认属性必须是对象属性或列表属性。</p>
<a name="running-the-example"></a>
<h2 id="running-the-example"><span lang="zh-cn">运行该示例</span></h2>
<p>示例中的main.cpp文件包含一个简单的shell应用程序，该应用程序加载并运行本页开头显示的QML代码片段。</p>
<p><span lang="zh-cn">文件：</span></p>
<ul>
<li><a href="qtqml-referenceexamples-default-birthdayparty-cpp.html">referenceexamples/default/birthdayparty.cpp</a></li>
<li><a href="qtqml-referenceexamples-default-birthdayparty-h.html">referenceexamples/default/birthdayparty.h</a></li>
<li><a href="qtqml-referenceexamples-default-default-pro.html">referenceexamples/default/default.pro</a></li>
<li><a href="qtqml-referenceexamples-default-default-qrc.html">referenceexamples/default/default.qrc</a></li>
<li><a href="qtqml-referenceexamples-default-example-qml.html">referenceexamples/default/example.qml</a></li>
<li><a href="qtqml-referenceexamples-default-main-cpp.html">referenceexamples/default/main.cpp</a></li>
<li><a href="qtqml-referenceexamples-default-person-cpp.html">referenceexamples/default/person.cpp</a></li>
<li><a href="qtqml-referenceexamples-default-person-h.html">referenceexamples/default/person.h</a></li>
</ul>
</div>
<!-- @@@referenceexamples/default -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
